package com.sky.controller.common;

import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

@Slf4j
@Controller("commonShopController")
public class CommonShopController {
    private static final String KEY = "SHOP_STATUS";

    private final RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    public CommonShopController(RedisTemplate<Object, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public Result<Integer> getStatus(){
        Integer shopStatus = (Integer) redisTemplate.opsForValue().get(KEY);
        if (shopStatus != null) {
            log.info("获取当前店铺营业状态: {}", shopStatus == 1 ? "营业中" : "打样中");
        } else {
            log.warn("无法获取店铺营业状态,已默认设置为打样中");
            redisTemplate.opsForValue().set(KEY,0);
            shopStatus = (Integer) redisTemplate.opsForValue().get(KEY);
        }
        return Result.success(shopStatus);
    }
}
